#include "utils.h"
#include "osd_linux_snd.h"
#include "kos.h"

snd_stream_hnd_t snd_stream;
//uint16 *stream;
uint16 stream[65536+16384];
int snd_started=0;

extern void *callback(snd_stream_hnd_t hnd, int smp_req, int *len);

#if !defined(SDL_mixer)
//  extern void sdl_fill_audio(void *data, Uint8 *stream, int len);
#else //SDL_mixer
  #include "osd_linux_sdl_music.h"
#endif

void osd_snd_set_volume(UChar v)
{
#if !defined (SDL_mixer)
	//#warning implement set volume for sdl

#else //SDL_mixer
	Uint8 vol;
	vol=v/2+((v==0)?0:1);// v=0 <=> vol=0; v=255 <=> vol=128
	Mix_Volume(-1,vol);
	Log("set volume %c\n",vol);
#endif
}


int osd_snd_init_sound(void)
{

    printf("osd_linux_snd.c init sound\n");

//	snd_init();



	snd_stream_init();
	snd_stream	= snd_stream_alloc(callback, 16384);
//	snd_stream_set_callback(snd_stream, callback);
//	stream=malloc(sizeof(SND_STREAM_BUFFER_MAX/16));
//	snd_stream_start(snd_stream, 22050, 1);

	memset(stream,0,65536+16384);

	host.sound.stereo = 1;
	host.sound.sample_size = 16;
	host.sound.freq = 22050;
	host.sound.signed_sound = 1;

  return 1;
}


void osd_snd_trash_sound(void)
{
  UChar chan;

	snd_stream_shutdown();
	snd_shutdown();

  for (chan = 0; chan < 6; chan++)
    memset(sbuf[chan], 0, SBUF_SIZE_BYTE);

  memset(adpcmbuf, 0, SBUF_SIZE_BYTE);
}


